//
//  ZYCManageStuAttendViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/18.
//

#import "ZYCManageStuAttendViewController.h"
#import "ZYCApplianceRootCell.h"
#import "ZYCStuLeaveRecordRootViewController.h"
#import "ZYCManagmentRecordViewController.h"
#import "ZYCStuAttendanceStatisticsViewController.h"
#import "ZYCApplianceRootModel.h"

@interface ZYCManageStuAttendViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *dataSource;



@end

@implementation ZYCManageStuAttendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"学生考勤";
    [self.view addSubview:self.collectionView];
    
    self.dataSource = [NSArray yy_modelArrayWithClass:[ZYCApplianceRootModel class] json:self.dataArray].mutableCopy;
    [self.collectionView reloadData];
}



#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZYCApplianceRootCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCApplianceRootCell class]) forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.item];
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(nonnull UICollectionViewCell *)cell forItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    UIBezierPath *maskPath;
    CGRect boardRect;
    float h = cell.bounds.size.height;
    float w = cell.bounds.size.width;
    if (indexPath.item == 0) {
        //左上
        boardRect = CGRectMake(0, -1, w, h);
        maskPath = [[UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(7.5, 7.5)] bezierPathByReversingPath];
    }else if(indexPath.item  == 3){
        //右上
        boardRect = CGRectMake(0, -1, w, h);
        maskPath = [[UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(7.5, 7.5)] bezierPathByReversingPath];
    }else{
        //其余的渲染不加角
        boardRect = CGRectMake(0, -1, w, h);
        maskPath = [UIBezierPath bezierPathWithRect:boardRect];
    }
    for (int i = 0; i < cell.contentView.layer.sublayers.count; i++) {
        CALayer *layer = cell.contentView.layer.sublayers[i];
        if ([layer.name isEqualToString:@"maskLayer"]) {
            [layer removeFromSuperlayer];
        }
    }
    //填充颜色和划线
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.name = @"maskLayer";
    maskLayer.frame = boardRect;
    maskLayer.path = maskPath.CGPath;
//    maskLayer.strokeColor = HEX_RGB(@"#F0F0F0").CGColor;
    maskLayer.strokeColor = [UIColor whiteColor].CGColor;
//    maskLayer.lineDashPattern = @[@2, @2];
    maskLayer.lineWidth = 2.0f;
    maskLayer.fillColor = [UIColor whiteColor].CGColor;
    maskLayer.masksToBounds = YES;
    [cell.contentView.layer insertSublayer:maskLayer atIndex:0];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    ZYCApplianceRootModel *model = self.dataSource[indexPath.item];
    if ([model.menuId isEqualToString:@"540759249913159"] && [self.authorityArray containsObject:@"540759249913159"]) {//请假记录
        ZYCStuLeaveRecordRootViewController *vc = [ZYCStuLeaveRecordRootViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.menuId isEqualToString:@"540759249913157"] && [self.authorityArray containsObject:@"540759249913157"]) {//考勤记录
        ZYCManagmentRecordViewController *vc = [ZYCManagmentRecordViewController new];
        vc.type = ZYCStuAttendanceTypeRecord;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.menuId isEqualToString:@"540759249913160"] && [self.authorityArray containsObject:@"540759249913160"]) {//考勤统计
        ZYCStuAttendanceStatisticsViewController *vc = [ZYCStuAttendanceStatisticsViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.menuId isEqualToString:@"540759249913161"] && [self.authorityArray containsObject:@"540759249913161"]) {//考勤管理
        ZYCManagmentRecordViewController *vc = [ZYCManagmentRecordViewController new];
        vc.type = ZYCStuAttendanceTypeManagement;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self tipShow];
    }

}
- (void)tipShow{
    [MrjToast makeToast:@"暂无权限访问" makeView:self.view];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:self.layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = YES;
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [_collectionView registerClass:[ZYCApplianceRootCell class]            forCellWithReuseIdentifier:NSStringFromClass([ZYCApplianceRootCell class])];
        _collectionView.showsVerticalScrollIndicator = NO;

    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat w = (kScreenWidth - 18) / 4;
        _layout.itemSize = CGSizeMake(w,w + 20);
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.headerReferenceSize = CGSizeMake(kScreenWidth, 10.0f);
        _layout.minimumLineSpacing = 0.0;//行间距
        _layout.minimumInteritemSpacing = 0.0f;//列间距
        _layout.sectionInset = UIEdgeInsetsMake(0, 9, 0, 9);
    }
    return _layout;
}



- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSArray alloc] init];
    }
    return _dataSource;
}
- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[
            @{@"menuId":@"540759249913159",@"menuName":@"请假记录",@"imgName":@"xsqjjj"},
            @{@"menuId":@"540759249913157",@"menuName":@"考勤记录",@"imgName":@"kqgl(1)"},
            @{@"menuId":@"540759249913160",@"menuName":@"考勤统计",@"imgName":@"kqtj"},
            @{@"menuId":@"540759249913161",@"menuName":@"考勤管理",@"imgName":@"kqgl"}
        ];
    }
    return _dataArray;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
